package io.github.yfblock.haustcsdnnew.controller;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import io.github.yfblock.haustcsdnnew.entity.Member;
import io.github.yfblock.yfSql.Runner.MysqlRunner;
import io.github.yfblock.yfSql.Sql.DataTableWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;


import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("member")
public class MemberController {

    @Autowired
    JavaMailSender javaMailSender;
    @Autowired
    FreeMarkerConfigurer freemarkerConfigurer;

    DataTableWrapper<Member> dataTableWrapper = new DataTableWrapper<Member>(Member.class, new MysqlRunner("root", "root", "java-orm-test"));

    // 添加用户
    @RequestMapping("/add")
    public String add(@RequestBody Member member) throws MessagingException, IOException, TemplateException {
//        this.sendTo("321353225@qq.com");

        // 判断email是否已经在数据库中存在， 如果已经存在则提示无法重复提交
        Member existMember = dataTableWrapper.where("email", member.getEmail()).find();
        if(existMember != null) return "无需重复提交";
        // 如果添加不成功 return
        if(dataTableWrapper.add(member) < 1) return "Error";
        Map<String, Object> params = new HashMap<>();
        params.put("name", member.getName());
        this.sendHtmlMailTo(member.getEmail(), params);
        return "ok";
    }

    @RequestMapping("/get")
    public Member get() {
        return dataTableWrapper.find();
    }

    // 发送普通邮件
    public void sendTo(String targetTo) {
        // 构建一个邮件对象
        SimpleMailMessage message = new SimpleMailMessage();
        // 设置邮件主题
        message.setSubject("这是一封测试邮件");
        // 设置邮件发送者，这个跟application.yml中设置的要一致
        message.setFrom("csdn@fzhuangyu.top");
        // 设置邮件接收者，可以有多个接收者，中间用逗号隔开，以下类似
        // message.setTo("10*****16@qq.com","12****32*qq.com");
        message.setTo(targetTo);
        // 设置邮件发送日期
        message.setSentDate(new Date());
        // 设置邮件的正文
        message.setText("这是测试邮件的正文");
        // 发送邮件
        javaMailSender.send(message);
    }

    // 发送html邮件   html模板为tem.ftl freemarker引擎  targetTo 为要发送到的邮箱 model为要替换的数据 参见freemarker 模板渲染
    public void sendHtmlMailTo(String targetTo, Map<String, Object> model) throws MessagingException, IOException, TemplateException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setSubject("这是一封测试邮件");
        helper.setFrom("csdn@fzhuangyu.top");
        helper.setTo(targetTo);
        helper.setSentDate(new Date());
        Template template = null;
        StringWriter swriter = new StringWriter();

        Configuration configuration = freemarkerConfigurer.getConfiguration();

        template = configuration.getTemplate("/tem.ftl");
        template.process(model, swriter);

        String process = swriter.toString();
        // 第二个参数true表示这是一个html文本
        helper.setText(process,true);
        javaMailSender.send(mimeMessage);
    }
}
